package com.aoip.telemetry.telemetry.configureui.business.commonconfig.mergeruledetail.controller;

import com.aoip.telemetry.telemetry.configureui.business.commonconfig.mergeruledetail.service.MergeRuleDetailService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.configureui.common.utils.poi.ExcelUtil;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.merge.DataMergeRuleDetailEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/10/17 22:29
 */
@RestController
public class MergeRuleDetailController  extends BaseController {

    @Resource
    private MergeRuleDetailService service;

    /**
     * 获取厂站列表带分页
     *
     * @param entity 厂站实体
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2022/4/23
     **/
    @GetMapping("/v1/GET/merge/rule/detail/page")
    public TableDataInfo page(DataMergeRuleDetailEntity entity) {
        startPage();
        List<DataMergeRuleDetailEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 厂站列表，不带分页
     *
     * @param entity 厂站实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/23
     **/
    @GetMapping("/v1/GET/merge/rule/detail/list")
    public AjaxResult list(DataMergeRuleDetailEntity entity) {
        List<DataMergeRuleDetailEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 查询厂站单条信息
     *
     * @param id 数据id
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/23
     **/
    @GetMapping("/v1/GET/merge/rule/detail/{id}")
    public AjaxResult detail(@PathVariable String id) {
        return AjaxResult.success(service.detail(id));
    }

    @Log(title = "厂站", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/merge/rule/detail")
    public AjaxResult add(@Validated @RequestBody DataMergeRuleDetailEntity entity) {
        return toAjax(service.insert(entity));
    }

    @Log(title = "厂站", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/merge/rule/detail")
    public AjaxResult edit(@Validated @RequestBody DataMergeRuleDetailEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化缓存数据到redis中，数据格式（id, DataMergeRuleDetailEntity）,（数据Id，厂站实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/merge/rule/detail/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    @Log(title = "厂站", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/merge/rule/detail/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        service.delete(ids);
        return success();
    }

    @Log(title = "厂站", businessType = BusinessType.EXPORT)
    @PostMapping("/v1/POST/merge/rule/detail/export")
    public void export(HttpServletResponse response, DataMergeRuleDetailEntity entity) {
        List<DataMergeRuleDetailEntity> list = service.list(entity);
        ExcelUtil<DataMergeRuleDetailEntity> util = new ExcelUtil<>(DataMergeRuleDetailEntity.class);
        util.exportExcel(response, list, "解析元素数据");
    }
}
